package com.exam.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.exam.demo.common.Result;
import com.exam.demo.controller.dto.UserDTO;
import com.exam.demo.entity.Admin;
import com.exam.demo.entity.Student;
import com.exam.demo.entity.Teacher;
import com.exam.demo.mapper.UserDTOMapper;
import com.exam.demo.service.AdminService;
import com.exam.demo.service.StudentService;
import com.exam.demo.service.TeacherService;
import com.exam.demo.service.UserService;
import com.exam.demo.util.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDTOMapper, UserDTO> implements UserService {

    @Resource
    StudentService studentService;
    @Resource
    TeacherService teacherService;
    @Resource
    AdminService adminService;


    @Override
    public UserDTO getbyUserId(String userId) {
        
        //按id的长度分不同角色
        if(userId.length() == 13)  {
            //学生
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("student_id",userId);
            Student one = studentService.getOne(studentQueryWrapper);
            if(one !=null) {
                UserDTO userDTO = ObjectMapper.studentToUserDTO(one);
                return userDTO;
            }
            return null;
        }else if(userId.length() == 10){
            //教师
            QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("teacher_id",userId);
            Teacher one = teacherService.getOne(teacherQueryWrapper);
            if(one !=null) {
                UserDTO userDTO = ObjectMapper.teacherToUserDTO(one);
                return userDTO;
            }
            return null;
        }else if(userId.length() == 6){
            //管理员
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("admin_id",userId);
            Admin one = adminService.getOne(adminQueryWrapper);
            if(one !=null) {
                UserDTO userDTO = ObjectMapper.adminToUserDTO(one);
                return userDTO;
            }
            return null;
        }

        return null;
    }
}
